package top.wuyang.study.javawebjdbc.workgonggao;

import top.wuyang.study.javawebjdbc.dao.DBHelper;
import top.wuyang.study.javawebjdbc.worknews.TbNews;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class TbGonggaoDAO {

    public static int insert(TbGonggao tbGonggao) throws SQLException {
        Connection conn = DBHelper.getConnection();
        String sql = "insert into tb_gonggao (ggtitle, gginfo, ggauthor) values(?, ?, ?)";
        PreparedStatement ps = conn.prepareStatement(sql);
        ps.setString(1,tbGonggao.getGgtitle());
        ps.setString(2,tbGonggao.getGginfo());
        ps.setString(3,tbGonggao.getGgauthor());
        int i = ps.executeUpdate();
        conn.close();
        return i;

    }

    public static List<TbGonggao> query() throws SQLException {
        List<TbGonggao> list = new ArrayList<>();
        Connection conn = DBHelper.getConnection();
        String sql = "select * from tb_gonggao";
        PreparedStatement ps = conn.prepareStatement(sql);
        ResultSet rs = ps.executeQuery();
        while (rs.next()){
            TbGonggao tbGonggao = new TbGonggao();
            tbGonggao.setId(rs.getInt("id"));
            tbGonggao.setGgtitle(rs.getString("ggtitle"));
            tbGonggao.setGginfo(rs.getString("gginfo"));
            tbGonggao.setGgauthor(rs.getString("ggauthor"));
            tbGonggao.setGgdate(rs.getTimestamp("ggdate"));
            list.add(tbGonggao);
        }
        conn.close();
        return list;
    }

    public static int delete (Integer id) throws SQLException {
        Connection conn = DBHelper.getConnection();
        String sql = "delete from tb_gonggao where id = ?";
        PreparedStatement ps = conn.prepareStatement(sql);
        ps.setInt(1,id);
        int i = ps.executeUpdate();
        conn.close();
        return i;
    }



}
